package com.webapp.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webapp.model.Post;
import com.webapp.model.PostModel;

/**
 * Servlet implementation class NewPost
 */
public class NewPost extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewPost() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String post = request.getParameter("postContent");
		int author = (Integer.parseInt((String) request.getParameter("author")));
		int circle = (Integer.parseInt((String)request.getParameter("circle")));
		Post postObj = new Post(circle,author,post);
		PostModel pm = new PostModel();
		if(pm.createPost(postObj))
			response.sendRedirect("Circles.jsp?circle=postadded");
		else {
			response.sendRedirect("Circles.jsp?circle=errorpost");
		}
	}

}
